package lab6;

import java.util.Arrays;

public class MergeSort {


	public static int[] mergeSort(int[] a)
	{
		if (a.length==1) return a;
		int[] left = Arrays.copyOfRange(a, 0, a.length/2);
		int[] right = Arrays.copyOfRange(a, a.length/2, a.length);
		return MergeIntArrays.mergeSorted(mergeSort(left), mergeSort(right));
	}
	
	public static void mergeSort(int p, int r, int[] a)
	{
		if (p==r) return;
		int m = (p+r)/2;
		mergeSort(p,m,a);
		mergeSort(m+1,r,a);
		int[] left=new int[m-p+1];
		int[] right=new int[r-m];
		for (int i=p; i<=m; i++) left[i-p]=a[i];
		for (int i=m+1; i<=r; i++) right[i-m-1]=a[i];
		int[] res=MergeIntArrays.mergeSorted(left,right);
		for (int i =0; i<res.length; i++)
		{
			a[i+p]=res[i];
		}
	}
	
	public static void main(String[] args) {
		int[] a={-5, 9, 0, 99, 199, 214, 0, -632, 2, 24, -9};
		mergeSort(0, a.length-1, a);
//		a=mergeSort(a);
		System.out.println(Arrays.toString(a));
	}

}
